*This file imports the simulation results and creates the figures and tables in the paper

run SetWorkspace
clear all


************************************************************************************************************************
*Miscellaneous figures
************************************************************************************************************************

*Alternative TEs (Figure 4)
import delim gains_alternative_te_common.csv, delim(",") asdouble case(preserve) clear

*Naming and labeling variables
rename v1 gains_common
rename v2 te_common
replace gains_common = gains_common*100
twoway (scatter gains_common te_common, mc(navy8)), ytitle(Gains from Industrial Policy) xtitle (Common Trade Elasticity {&theta}) name(common_te) title("")
rename v3 gains_het
rename v4 sd_te
replace gains_het = gains_het*100
twoway (scatter gains_het sd_te, mc(navy8)), ytitle(Gains from Industrial Policy) xtitle (Standard Deviation of Trade Elasticity {&theta}) name(sd_te) title("")

*First make figure for rho and non-manufacturing robustness checks (Figure 5)
*Loading robustness checks with different rho

import delim gains_ces_soe_rob_rho.csv, delim(",") asdouble case(preserve) clear
rename v1 rho
rename v2 gfoip_rho_rob
rename v3 gfotp_rho_rob
replace gfoip_rho_rob=100*gfoip_rho_rob
replace gfotp_rho_rob=100*(gfotp_rho_rob)
twoway (scatter gfoip_rho_rob rho,mc(navy8)), ytitle (Gains from Industrial Policy) xtitle (Elasticity of substitution between sectors {1+ &rho}) name(gfoip_rho) title("")

*now different non-manufacturing gamma
import delim gains_ces_soe_rob_gamma.csv, delim(",") asdouble case(preserve) clear
rename v1 gamma_nm
rename v2 gfoip_gamma_nm_rob
rename v3 gfotp_gamma_nm_rob
replace gfoip_gamma_nm_rob=gfoip_gamma_nm_rob*100
replace gfotp_gamma_nm_rob=100*gfotp_gamma_nm_rob
twoway (scatter gfoip_gamma_nm_rob gamma_nm, mc(navy8)), ytitle(Gains from Industrial Policy) xtitle (Scale economies in non-manufacturing {&gamma}) name(gfoip_gamma_nm) title("")


*Now make graph of relative SEs with and without intermediates (Figure 6)
import delim scale_elasticities_all.csv, delim(",") asdouble case(preserve) clear
keep Industry se_baseline se_cap_int*
correlate se_baseline se_cap_int*

*Transform variables
gen policy_baseline = se_baseline/(1+se_baseline)
gen policy_cap_int_go = se_cap_int_go/(1+se_cap_int_go)
gen percent_diff = (policy_cap_int_go-policy_baseline)/policy_baseline
egen avdiff = mean(percent_diff)
correlate policy_baseline policy_cap_int_go

twoway (scatter policy_cap_int_go policy_baseline, mc(gray)) (lfit  policy_baseline policy_baseline, lw(medthick) lc(green) lp(dash) range(0 .3)), ytitle (Beyond Ricardian Economies) xtitle (Ricardian Economies) name(se_comparison) title("") legend(off)


*********************************************************************************************************************************
*Main Results
********************************************************************************************************************************

*main results, no intermediates
import delim gains_soe_hte_ces_0.csv, delim(",") asdouble case(preserve) clear

*Naming and labeling variables
rename v1 countryid
rename v2 gfop_0
rename v3 gftp_0
rename v4 gfip_0
rename v5 gfoip_0
rename v6 gft_0
rename v7 corr_se_va
rename v8 prod_se_va
rename v9 tot_reallocation
gen gfotp_0=gfop_0-gfip_0
capture drop v*
save temp, replace

import delim gains_soe_hte_ces_harberger.csv, delim(",") asdouble case(preserve) clear
rename v1 countryid
rename v2 gfop_harberger
rename v3 gfotp_harberger
rename v4 gfoip_harberger

merge m:1 countryid using temp, ///
	nogen keep(match)
	
save temp, replace


*Now importing the gains with intermediates
import delim gains_soe_hte_ces_cap_int_go_0.csv, delim(",") asdouble case(preserve) clear

rename v1 countryid
rename v2 gfop_cap_int_go_0
rename v3 gftp_cap_int_go_0
rename v4 gfip_cap_int_go_0
rename v5 gfoip_cap_int_go_0
rename v6 gfotp_cap_int_go_0

	merge m:1 countryid using temp, ///
	nogen keep(match)
	save temp, replace
	
*Now load harberger gains from intermediate analysis
import delim gains_harberger_cap_int_go.csv, delim(",") asdouble case(preserve) clear
rename v1 countryid
rename v2 gfop_harberger_cap_int_go
rename v3 gfotp_harberger_cap_int_go
rename v4 gfoip_harberger_cap_int_go

	merge m:1 countryid using temp, ///
	nogen keep(match)
	save temp, replace
	
	
**********************************************************************
*Now load in gains from twentieth century policy
import delim gains_20_cen_ces.csv, delim(",") asdouble case(preserve) clear
rename v1 countryid
rename v2 gfoip_20cen_0
capture drop v*

merge m:1 countryid using temp, ///
	nogen keep(match)
	
save temp, replace

*Now load gains from 21st century industrial policy
import delim gains_hte_ces_ge.csv, delim(",") asdouble case(preserve) clear
rename v1 countryid
rename v2 gfoip_21cen_0
capture drop v*

merge m:1 countryid using temp, ///
	nogen keep(match)
	
save temp, replace

*Now merging with trade and expenditure data
use simulation_data_long, clear

*Merge with parameters used in simulations

import delim sim_params_no_int.csv, delim(",") asdouble case(preserve) clear
rename v1 scale_elasticity
rename v2 trade_elasticity
gen sectorId=_n+2
capture drop v*

merge 1:m sectorId using simulation_data_long, ///
	nogen
replace trade_elasticity=4.32 if trade_elasticity==.
replace scale_elasticity=0 if scale_elasticity==.


*Computing some basic averages and correlations

*exports as a share of value added
*Imports as a share of expenditures
*Trade balance
*Manufacturing va as share of VA
*Manufacturing exports as share of man VA
egen tot_go = sum(X), by (originId)
egen tot_va=sum(value_added) if originId==destId,by(originId)
egen tot_expenditure=sum(expenditure) if originId==destId, by(originId)
egen vv=max(tot_va), by(originId sectorId)
replace tot_va=vv
drop vv
egen vv=max(tot_expenditure), by(originId sectorId)
replace tot_expenditure=vv
drop vv

gen exports=value_added-X if originId==destId
gen imports=expenditure-X if originId==destId


*Net exports
gen net_exports_va=(exports-imports)/value_added

egen x=max(exports), by(originId sectorId)
egen im=max(imports), by(originId sectorId)
replace exports=x
replace imports=im
drop x im
keep if originId==destId

*Computing correlation between sectoral net exports/va and 21st century policy
sort originId
egen corr_ne_21 = corr(net_exports_va scale_elasticity), by(originId)
*egen corr_ex_21 = corr(export_share scale_elasticity), by(originId)
gen va_share = value_added/tot_va
egen corr_va_21 = corr(va_share scale_elasticity), by(originId)

egen tot_exports=sum(exports),by(originId)
egen tot_imports = sum(imports),by(originId) 

egen world_va=sum(value_added)
gen worldva_share = tot_va/world_va

*Openness
replace trade_balance = trade_balance/tot_va
gen openness = (tot_exports+tot_imports)/(tot_go)


rename originId countryid

merge m:1 countryid using temp, ///
	nogen keep(match)

*Changing the gains from policy to percentages

foreach x of varlist gfop_0* gfop_cap* gftp* gfip*  gfoip_2*	{
	replace `x'=(round(`x',.0001)-1)*100
}
	
foreach x of varlist  gfoip_0* gfoip_cap* gfotp_cap* gft_* open* gfotp_0 gfoip_harberger*	gfotp_harberger* gfop_harberger*	{
	replace `x'=round(`x',.0001)*100
	}


keep countryid gfop* gftp* gfotp* gfoip_* gft* gfip* openness* open* worldva_share man_va_share corr_se_va prod_se_va tot_reallocation corr_ne_21 corr_va_21
duplicates drop

*Bring in country codes
save temp, replace

use ctry_codes, clear
rename countryId countryid

merge 1:1 countryid using temp, ///
	nogen keep(match)


save simulation_soe_results, replace

********************************************************************************

*Some averages and correlations
drop if countryid==62

foreach x of varlist gf*	{
	egen m`x'=mean(`x')
	}
	
	*Generating weighted averages

foreach x of varlist gfoip* gft* gfop* gfip* gfotp*	{
	gen w`x'=`x'*worldva_share
	egen mw`x'=sum(w`x')
	}
	
graph drop _all


*Making a new set of labels that reflect the country codes in our main table only
gen ISO_label = countryISO if countryISO=="USA"
replace ISO_label = countryISO if countryISO=="DEU"
replace ISO_label = countryISO if countryISO=="IRL"
replace ISO_label = countryISO if countryISO=="CHN"
replace ISO_label = countryISO if countryISO=="VNM"	
graph drop _all


********************** Making Graphs and Computing correlations for the Main Results


*Graph of openness vs. gains (Figure 3)
twoway (scatter gfoip_0 openness, mlabel(ISO_label) mc(gray) mlabc(black) ) (lfit  gfoip_0 openness, lw(medthick) lc(gray) ), ytitle (Exact Gains from Policy) xtitle (Openness) name(gfoip_openness_1) title("") legend(off)

*Graph of Harberger vs exact, no intermediates (Figure 2)
twoway (scatter gfoip_0 gfoip_harberger, mlabel(ISO_label) mc(gray) mlabc(black)) (lfit  gfoip_harberger gfoip_harberger, lw(medthick) lc(green) lp(dash) range(0 2.5)) (lfit  gfoip_0 gfoip_harberger, lw(medthick) lc(gray) range (0 2.5)), ytitle (Exact Gains from Policy) xtitle (Area of Harberger Triangles) ///
name(gfoip_harberger) title("") legend(off)

*Tests of accuracy of second order approximations
regress gfoip_0 gfoip_harberger
regress gfotp_0 gfotp_harberger
regress gfop_0 gfop_harberger

*Auxilary regressions
regress gfoip_0 tot_reallocation
regress gfoip_0 openness
regress gfoip_0 prod_se_va
regress gfoip_0 openness prod_se_va

*Correlation of 21st century policy gains with net export in high-se countries
correlate gfoip_21cen_0 corr_ne_21

*Graph comparing Harberger and Exact, with Intermediates (Figure C.1)
graph drop _all
twoway (scatter gfoip_cap_int_go_0  gfoip_harberger_cap_int_go, mlabel(ISO_label) mc(gray) mlabc(black)) (lfit  gfoip_cap_int_go_0  gfoip_harberger_cap_int_go, lw(medthick) lc(gray) range(0 15)) (lfit  gfoip_harberger_cap_int_go gfoip_harberger_cap_int_go, lw(medthick) lc(green) lp(dash) range(0 15)) , ///
ytitle (Exact Gains) xtitle (Area of Harberger Triangles) name(gfoip_harberger) title("") legend(off)
regress gfoip_cap_int_go_0 gfoip_harberger_cap_int_go

*Ratios
gen harberger_exact_cap_int = gfoip_cap_int_go_0/gfoip_harberger_cap_int_go
gen harberger_exact = gfoip_0/gfoip_harberger

egen m_harberger_exact_cap_int = mean(harberger_exact_cap_int)
egen m_harberger_exact= mean(harberger_exact)


***************************************
*Comparing gains with and without intermediates (Figure 7)
graph drop _all
twoway (scatter gfoip_cap_int_go_0 gfoip_0, mlabel(ISO_label) mc(gray) mlabc(black)) (lfit gfoip_cap_int_go_0 gfoip_0, lw(medthick) lc(gray) range(0 3) )(lfit gfoip_0 gfoip_0, range(0 10) lw(medthick) lc(green) lp(dash) ), ytitle (Beyond Ricardian Economies) xtitle (Ricardian Economies) name(gfoip_cap_int) title("") legend(off)
regress gfoip_0 gfoip_cap_int_go_0


************************************************************************************************
************************************************************************************************
*Making Tables


*Table of gains from optimal policy, baseline

preserve
keep country gfop_0 gfotp_0 gfoip_0
order country country gfop_0 gfotp_0 gfoip_0
sort country
outsheet using ${tab_dir}\gains_all_countries.csv, comma replace

restore

*Table of gains from 20th and 21st century policy

preserve
keep country gfoip_0 gfoip_20cen_0 gfoip_21cen_0
order country gfoip_0 gfoip_20cen_0 gfoip_21cen_0
sort country
outsheet using ${tab_dir}\gains_20_21_all_countries.csv, comma replace

restore

*Table of gains with capital and intermediates

preserve
keep country gfop_cap_int_go_0 gfotp_cap_int_go_0 gfoip_cap_int_go_0
order country gfop_cap_int_go_0 gfotp_cap_int_go_0 gfoip_cap_int_go_0

*foreach x of varlist gfop_cap_int_go_0 gfotp_cap_int_go_0 gfoip_cap_int_go_0	{
*replace `x'=round(`x',2)
*}
sort country
outsheet using ${tab_dir}\gains_cap_int_all_countries.csv, comma replace
restore











